package org.scohen.juploadr.uploadapi.flickrrest.authentication;

import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class GetTokenResponseHandler extends DefaultFlickrHandler {
    private GetToken parent;

    public GetTokenResponseHandler(Object parent) {
        super(parent);
        this.parent = (GetToken) parent;
    }

    public void startElement(String uri, String localName, String qname,
            Attributes atts) throws SAXException {
        super.startElement(uri, localName, qname, atts);
        lastTag = qname;
        if ("user".equals(qname)) {
            parent.setFullName(atts.getValue("fullname"));
            parent.setUsername(atts.getValue("username"));
            parent.setNsid(atts.getValue("ndsid"));
        }
    }

    public void characters(char[] chars, int start, int end)
            throws SAXException {
        String cdata = new String(chars, start, end).trim();

        if (cdata.length() > 0) {
            if ("token".equals(lastTag)) {
                parent.setToken(cdata);
            } else if ("perms".equals(lastTag)) {
                parent.setPermissions(cdata);
            }
        }
    }

}
